package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
//这是定时器类, 用于数据落地
public class StatsDataPersistenceJob  {

    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelStatsCacheService travelStatsCacheService;
    @Reference
    private ITravelService travelService;

    @Scheduled(cron = "* 0/5 * * * ? ") //周期性地执行下面的方法
    public void persistence(){
        //拿到所有的统计数据封装成对象放到集合里面
        List<StrategyStats> list = strategyStatsCacheService.listAllStats();
        for (StrategyStats stats : list) {
            StrategyDetail detail = new StrategyDetail();
            detail.setId(stats.getStrategyId());
            detail.setViewnum(stats.getViewnum());
            detail.setReplynum(stats.getReplynum());
            detail.setFavornum(stats.getFavornum());
            detail.setThumbsupnum(stats.getThumbsupnum());
            //更新到mysql
            strategyDetailService.updateDetail(detail);
        }
        //拿到所有的统计数据封装成对象放到集合里面
        List<TravelStats> travelStats = travelStatsCacheService.listAllStats();
        for (TravelStats stats : travelStats) {
            Travel travel = new Travel();
            travel.setId(stats.getTravelId());
            travel.setViewnum(stats.getViewnum());
            travel.setReplynum(stats.getReplynum());
            travel.setFavornum(stats.getFavornum());
            travel.setThumbsupnum(stats.getThumbsupnum());
            //更新到mysql
            travelService.updateTravelStats(travel);
        }
    }
}
